package com.lehecai.core.lottery.fetcher.lotterydraw.impl;

import com.lehecai.core.lottery.LotteryType;
import com.lehecai.core.lottery.fetcher.FetcherType;
import com.lehecai.core.lottery.fetcher.lotterydraw.LotteryDraw;
import com.lehecai.core.lottery.fetcher.lotterydraw.impl.worker.AbstractLotteryDrawFetchWorker;
import com.lehecai.core.lottery.fetcher.lotterydraw.impl.worker.LotteryDrawFetch544Worker163;
import com.lehecai.core.util.CoreFetcherUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Calendar;

/**
 * Created by zhangzq on 14-8-11.
 */
public class LotteryDrawFetcher544 extends BaseLotteryDrawFetcher {

    private static final LotteryType lotteryType = LotteryType.GDKL10;

    protected LotteryDraw fetch163(String phase) {
        if (StringUtils.isNotEmpty(phase) && phase.length() == 10) {
            phase = phase.substring(2);
        }
        AbstractLotteryDrawFetchWorker fetchWorker = new LotteryDrawFetch544Worker163(lotteryType);
        LotteryDraw resultListObj = fetchWorker.fetchResult(phase);
        LotteryDraw resultDetailObj = fetchWorker.fetchResultDetail(phase);
        //如果传入的彩期没取得详情开奖信息 则提示未抓取成功
        if (resultDetailObj == null) {
            return null;
        }
        if (resultListObj != null && StringUtils.isNotEmpty(resultListObj.getPhase())) {
            Calendar calendar= Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            String yearStr = year + "";
            resultListObj.setPhase(yearStr.substring(0, 2) + resultListObj.getPhase());
        }
        if (StringUtils.isNotEmpty(resultDetailObj.getPhase())) {
            Calendar calendar= Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            String yearStr = year + "";
            resultDetailObj.setPhase(yearStr.substring(0, 2) + resultDetailObj.getPhase());
        }
        LotteryDraw returnObj = CoreFetcherUtils.getComparedResult(resultListObj, resultDetailObj);
        return returnObj;
    }

    @Override
    protected FetcherType getDefaultFetcherType() {
        return FetcherType.T_163;
    }
}
